//
//  OptionalSelectView.m
//  OptionalSelectView
//
//  Created by 正合适 on 2017/6/16.
//  Copyright © 2017年 ShenTong. All rights reserved.
//

#import "OptionalSelectView.h"
#import <ZHToolKit/ZHToolKit.h>

#define kBaseTag 100
#define kRGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]
#define kRGBColor(r,g,b)    kRGBAColor((r),(g),(b),1)

@interface OptionalSelectView ()
/**<#desc#>*/
@property (nonatomic, strong) NSMutableArray *optionalArr;
/**uiv*/
@property (nonatomic, strong) UIView *lineView;
/**<#desc#>*/
@property (nonatomic, assign) NSInteger lastIndex;
/**配置文件*/
@property (nonatomic, strong) OptionalSelectConfig *config;
@end

@implementation OptionalSelectView

- (instancetype)initWithFrame:(CGRect)frame optional:(NSArray <NSString *>*)optionalArr config:(OptionalSelectConfig *)config {
    self = [super initWithFrame:frame];
    if (self) {
        _config = config;
        _lastIndex = 0;
        _optionalArr = [NSMutableArray array];
        _optionalArr = optionalArr.mutableCopy;
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame optional:(NSArray <NSString *>*)optionalArr {
    return [self initWithFrame:frame optional:optionalArr config:nil];
}

#pragma mark - 初始化界面
- (void)createUI {
    if (_optionalArr.count > 0) {
        float width = self.width/_optionalArr.count;
        float oX = 0;
        float oY = 0;
        UIColor *lineColor = kRGBColor(42, 135, 226);
        if (_config.lineColor) {
            lineColor = _config.lineColor;
        }
        NSString *title = @"";
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-2.5, width, 2)];
        _lineView.backgroundColor = lineColor;
        [self addSubview:_lineView];
        
        UIColor *btnNormalColor = kRGBColor(102, 102, 102);
        if (_config.titleNormalColor) {
            btnNormalColor = _config.titleNormalColor;
        }
        UIColor *btnSelectedColor = kRGBColor(42, 135, 226);
        if (_config.titleSelectedColor) {
            btnSelectedColor = _config.titleSelectedColor;
        }
        UIFont *btnFont = [UIFont systemFontOfSize:18];
        if (_config.font) {
            btnFont = _config.font;
        }
        for (NSInteger i = 0; i < _optionalArr.count; i ++) {
            title = _optionalArr[i];
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(oX, oY, width, self.height)];
            [btn setTitle:title forState:UIControlStateNormal];
            [btn setTitleColor:btnNormalColor forState:UIControlStateNormal];
            btn.titleLabel.font = btnFont;
            btn.tag = kBaseTag+i;
            [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            oX = btn.right;
            if (i == 0) {
                [btn setTitleColor:btnSelectedColor forState:UIControlStateNormal];
            }
        }
    }
}

- (void)buttonAction:(UIButton *)sender {
    UIColor *btnNormalColor = kRGBColor(102, 102, 102);
    if (_config.titleNormalColor) {
        btnNormalColor = _config.titleNormalColor;
    }
    UIColor *btnSelectedColor = kRGBColor(42, 135, 226);
    if (_config.titleSelectedColor) {
        btnSelectedColor = _config.titleSelectedColor;
    }
    if (_lastIndex == sender.tag - kBaseTag) {
        return;
    }
    self.userInteractionEnabled = NO;
    _lastIndex = sender.tag-kBaseTag;
    for (UIButton *btn in self.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            [btn setTitleColor:btnNormalColor forState:UIControlStateNormal];
        }
    }
    [sender setTitleColor:btnSelectedColor forState:UIControlStateNormal];
    
    float time = fabs(sender.left-_lineView.left) * 0.5/self.width;
    [UIView animateWithDuration:time animations:^{
        _lineView.origin = CGPointMake(sender.left, _lineView.origin.y);
    } completion:^(BOOL finished) {
        self.userInteractionEnabled = YES;
    }];
    if (self.optionalSelectViewBlock) {
        self.optionalSelectViewBlock(sender.tag-kBaseTag);
    }
}

- (void)configTitlesWith:(NSArray <NSString *>*)titles {
    for (NSInteger i = 0; i < titles.count; i ++) {
        UIButton *btn = [self viewWithTag:(kBaseTag+i)];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
    }
}

@end
